package com.bj58.groupbuy.action.service.impl;

import java.util.List;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.model.DisplayLocal;
import com.bj58.groupbuy.action.service.DisplayLocalService;
import com.bj58.groupbuy.action.util.MyStore;

public class DisplayLocalServiceImpl implements DisplayLocalService {
	private CommonDao  cmDao = new CommonDao();
	
	@Override
	public List<DisplayLocal> getLevelOne() throws Exception {
		String sql = "select * from data_dict.m_common_disp_city " +
				"where city2_name is null and city_id >0 order by city_name_en ";
		return cmDao.getAllColsByVo(sql, DisplayLocal.class, DBConfig.db_131_data_bi.getIntType());
	}

	@Override
	public List<DisplayLocal> getLevelTwo(String city1) throws Exception {
		String sql = "SELECT * FROM data_dict.m_common_disp_city  where city1_name = '"+city1+"'  " +
				"and city2_name is not null and city3_name is null  order by city_name_en ";
		return cmDao.getAllColsByVo(sql, DisplayLocal.class, DBConfig.db_131_data_bi.getIntType());
	}

	@Override
	public List<DisplayLocal> getLevelThree(String city1,
			String city2) throws Exception {
		String sql = "SELECT * FROM data_dict.m_common_disp_city  " +
				"where city1_name = '"+city1+"'  and city2_name ='"+city2+"'  " +
				"and city3_name is not null and city4_name is null order by city_name_en ";
		return cmDao.getAllColsByVo(sql, DisplayLocal.class, DBConfig.db_131_data_bi.getIntType());
	}

	@Override
	public DisplayLocal getOneById(long localId) throws Exception {
		String sql = "select * from data_dict.m_common_disp_city where  city_id =" + localId;
		List<DisplayLocal> tempList =  cmDao.getAllColsByVo(sql, DisplayLocal.class, DBConfig.db_131_data_bi.getIntType());
		if(null == tempList || tempList.isEmpty()){
			return null;
		}
		return tempList.get(0);
	}

	@Override
	public List<DisplayLocal> getAll() throws Exception {
		String sql = "select * from data_dict.m_common_disp_city where city_id > 0 ";
		List<DisplayLocal> tempList =  cmDao.getAllColsByVo(sql, DisplayLocal.class, DBConfig.db_131_data_bi.getIntType());
		if(null == tempList || tempList.isEmpty()){
			return null;
		}
		return tempList;
	}

	@Override
	public List<DisplayLocal> getCitysByOpers(String operCitys)
			throws Exception {
		
		//全部城市权限
		if(MyStore.cityOperAll.equals(operCitys)){
			return getLevelOne();
		}
		
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from data_dict.m_common_disp_city  ");
		sql.append(" where city_id > 0 ");
		sql.append(" and city2_name is null ");
		//直营城市权限
		if(MyStore.cityOperZY.equals(operCitys)){
			sql.append(" and city_name in("+ MyStore.zycitys + ") ");
		}else if(MyStore.cityOperQD.equals(operCitys)){//所有渠道城市
			sql.append(" and city_name not in("+ MyStore.zycitys + ") ");
		}else{
			operCitys = operCitys.substring(0,operCitys.lastIndexOf(","));
			sql.append(" and city_name in("+ operCitys + ") ");
		}
		sql.append(" order by city_name_en ");	
		
		return cmDao.getAllColsByVo(sql.toString(), DisplayLocal.class, DBConfig.db_131_data_bi.getIntType());
	}

}
